package com.nsy.controller.user;

import com.github.pagehelper.PageInfo;
import com.nsy.entity.Result;
import com.nsy.pojo.SysUser;
import com.nsy.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    public ISysUserService sysUserService;
    //跳转到登录页面
    @RequestMapping("/to_Login")
    public String toLogin() {
        return "login";
    }
    //登录验证
    @RequestMapping("/login")
    public String login(SysUser sysUser, Model model, HttpSession httpSession) {
        System.out.println("收到数据");
        System.out.println(sysUser);
        //安全验证
        if (sysUser ==null||sysUser.getUsername()==null
                ||sysUser.getUsername().trim().equals("")
                ||sysUser.getPassword()==null
                ||sysUser.getPassword().trim().equals("")){
            model.addAttribute("msg","用户名或密码不能为空!");
            return "login";
        }
        SysUser user = sysUserService.getUserByUsernameAndPwd(sysUser);
        if (user != null) {
            httpSession.setAttribute("USER_SESSION",user);
            model.addAttribute("name", user.getName());
            return "main";
        }else {
            model.addAttribute("msg","用户名或密码错误!");
            return "login";
        }
    }
    //跳转到主页面
    @RequestMapping("/main")
    public String main(HttpSession httpSession,Model model) {
        SysUser user = (SysUser)httpSession.getAttribute("USER_SESSION");
        model.addAttribute("name", user.getName());
        return "main";
    }
    //跳转到首页
    @RequestMapping("/index")
    public String index() {
        return "index";
    }
    //用户列表页面
    @RequestMapping("/userList")
    public String userList(String username, Model model ,
    @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
    @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize){
        List<SysUser> allUsers = sysUserService.getAllUsers(username, pageNum, pageSize);
        PageInfo<Object> pageInfo = new PageInfo<>(allUsers);
        //System.out.println(pageInfo);
        model.addAttribute("userList",allUsers);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("username",username);
        return "userList";
    }
    //跳转到添加用户页面
    @RequestMapping("/addUser")
    @ResponseBody
    public Result addUser(SysUser sysUser){
        Result<Object> result = new Result<>();
        if (sysUser!=null
                && sysUser.getUsername()!=null
                && !sysUser.getUsername().trim().equals("")
                && sysUser.getPassword()!=null
                && !sysUser.getPassword().trim().equals("")
        ){
            System.out.println(sysUser);
            //是否重名
            SysUser user = sysUserService.getUserByUsername(sysUser.getUsername());
            if (user!=null){
                result.setMsg("用户已经存在！");
                result.setSuccess(false);
                return result;
            }
            int i = sysUserService.addUser(sysUser);
            if (i>0){
                System.out.println("新增成功！");
                result.setMsg("新增成功！");
                result.setSuccess(true);
                return result;
            }else {
                System.out.println("新增失败！");
                result.setMsg("新增失败！");
                result.setSuccess(false);
                return result;
            }

        }else{
            System.out.println("请输入用户名和密码！");
            result.setMsg("请输入用户名和密码！");
            result.setSuccess(false);
            return result;
        }

    }
    //删除用户
    @RequestMapping("/delUser")
    @ResponseBody
    public Result delUser(@RequestParam("id") Integer id){
        Result<Object> result = new Result<>();
        if(id != null){
            int i = sysUserService.deleteUser(id);
            if (i > 0) {
                System.out.println("删除成功！");
                result.setMsg("删除成功！");
                result.setSuccess(true);
                return result;
            } else {
                System.out.println("删除失败！");
                result.setMsg("删除失败！");
                result.setSuccess(false);
                return result;
            }

        }else{
            System.out.println("请输入id！");
            result.setMsg("请输入id！");
            result.setSuccess(false);
            return result;
        }
    }

    //批量删除
    @RequestMapping("/delAllUser")
    @ResponseBody
    public Result delAllUser(String ids){
        Result<Object> result = new Result<>();
        System.out.println("收到删除数据");
        System.out.println(ids);

        if (ids!=null){
            String[] strArray = ids.split(",");
            List<String> idsList = Arrays.asList(strArray);

            int i = sysUserService.deleteAllUser(idsList);
            if (i>0){
                System.out.println("删除成功！");
                result.setMsg("删除成功！");
                result.setSuccess(true);
                return result;
            }else {
                System.out.println("删除失败！");
                result.setMsg("删除失败！");
                result.setSuccess(false);
                return result;
            }

        }else{
            System.out.println("请选择id！");
            result.setMsg("请选择id！");
            result.setSuccess(false);
            return result;
        }
    }
    //根据id查询用户
    @RequestMapping("/getUserById")
    @ResponseBody
    public Result getUserById(Integer id){
        Result<Object> result = new Result<>();
        System.out.println("收到查询数据");
        if(id != null){
            SysUser user = sysUserService.getUserById(id);
            if (user != null){
                result.setData(user);
                System.out.println("查找成功！");
                result.setMsg("查找成功！");
                result.setSuccess(true);
                return result;
            } else {
                System.out.println("没有此用户！");
                result.setMsg("没有此用户！");
                result.setSuccess(false);
                return result;
            }

        }else{
            System.out.println("请输入id！");
            result.setMsg("请输入id！");
            result.setSuccess(false);
            return result;
        }
    }
    /**
     * 修改密码
     * @param oldPassword 旧密码
     * @param newPassword 新密码
     * @return 返回包含操作结果信息的Result对象
     */
    @RequestMapping("/updatePassword")
    @ResponseBody
    public Result updatePassword(String oldPassword, String newPassword) {
        Result<Object> result = new Result<>();
        if (oldPassword!= null && newPassword!= null &&!newPassword.trim().equals("")) {
            boolean success = sysUserService.updatePassword(oldPassword, newPassword);
            if (success) {
                result.setMsg("密码修改成功！");
                result.setSuccess(true);
            } else {
                result.setMsg("密码修改失败！");
                result.setSuccess(false);
            }
        } else {
            result.setMsg("请输入正确的旧密码！");
            result.setSuccess(false);
        }
        return result;
    }
   //编辑用户
   @RequestMapping("/editUser")
   @ResponseBody
   public Result editUser(SysUser sysUser){
       Result<Object> result = new Result<>();
       if (sysUser!=null
               && sysUser.getUsername()!=null
               && !sysUser.getUsername().trim().equals("")
               && sysUser.getId()!=null
       ){
           System.out.println(sysUser);
           int i = sysUserService.editUser(sysUser);
           if (i>0){
               System.out.println("修改成功！");
               result.setMsg("修改成功！");
               result.setSuccess(true);
               return result;
           }else {
               System.out.println("修改失败！");
               result.setMsg("修改失败！");
               result.setSuccess(false);
               return result;
           }

       }else{
           System.out.println("请输入用户名或id！");
           result.setMsg("请输入用户名或id！");
           result.setSuccess(false);
           return result;
       }

   }

    //用户详情
    @RequestMapping("/detailUser")
    @ResponseBody
    public Result detailUser(Integer id){
        Result<Object> result = new Result<>();
        System.out.println("收到查询数据");
        if(id != null){
           SysUser sysUser = sysUserService.detailUser(id);
            if (sysUser!=null){
                result.setData(sysUser);
                System.out.println("查找成功！");
                result.setMsg("查找成功！");
                result.setSuccess(true);
                return result;
            } else {
                System.out.println("没有此用户！");
                result.setMsg("没有此用户！");
                result.setSuccess(false);
                return result;
            }

        }else{
            System.out.println("请输入id！");
            result.setMsg("请输入id！");
            result.setSuccess(false);
            return result;
        }
    }
    //注销
    @RequestMapping(value = "/logout", method = RequestMethod.GET)
    public String logout(HttpSession session) {
        // 清除会话中的用户信息
        session.removeAttribute("userInfo");
        // 使session无效
        session.invalidate();
        return "redirect:/login"; // 重定向到登录页面
    }
}
